package com.rjy.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rjy.entity.Query;
import com.rjy.entity.Student;
import com.rjy.entity.Teacher;
import com.rjy.entity.User;
import com.rjy.mapper.UserMapper;
import com.rjy.service.MyMailService;
import com.rjy.util.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CrossOrigin(origins = "*")
@RestController
public class UserController {
    @Autowired
    private Code code;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MyMailService myMailService;
    StringBuilder strcode=null;
    @CrossOrigin(origins = "*")
    @RequestMapping("/regist")
    public String regist(@RequestParam("name")String name,
                         @RequestParam("password")String password,
                         @RequestParam("email")String email,
                         @RequestParam("vercode")String vercode){
        if (strcode.toString().equals(vercode)){
            User user = new User();
            user.setName(name);
            user.setPassword(password);
            user.setEmail(email);
            userMapper.insert(user);
            return "success";
        }else {
            return "error";
        }


    }
    @CrossOrigin(origins = "*")
    @RequestMapping("/loginUser")
    public String loginUser(@RequestParam("name")String name,
                        @RequestParam("password")String password){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("name",name).eq("password",password);
        User user =userMapper.selectOne(userQueryWrapper);
        if (user!=null){
            return "success";
        }else{
            return "error";
        }
    }

    @RequestMapping("/sendEmail")
    public String sendEmail(@RequestParam("to")String to,
                            @RequestParam("title")String title,
                            @RequestParam("text")String text){
        myMailService.sendMail("2928237433@qq.com",to,title,text);
        return "发送成功";
    }

    @RequestMapping("/sendEmail2")
    public void sendEmail2(@RequestParam("name")String name){
            strcode=code.getCode();
            String text = "你的验证码为:"+strcode+"请勿泄露";
        myMailService.sendMail("2928237433@qq.com",
                name,"注册验证码",text);
    }

}
